package reflect;

import java.util.Scanner;

/**
 * 使用类对象实例化
 */
public class ReflectDemo02 {
    public static void main(String[] args) throws ClassNotFoundException, IllegalAccessException, InstantiationException {
        Person p = new Person();
        System.out.println(p);

        /*
           反射机制进行实例化的步骤:
           1:加载要实例化对象的类的类对象
           2:直接通过类对象的方法newInstance()实例化
         */
//        Class cls = Class.forName("reflect.Person");
//        //newInstance()方法回调出类对象所表示的类公开的无参构造器实例化
//        Object obj = cls.newInstance();//new Person()
//        System.out.println(obj);


        Scanner scan = new Scanner(System.in);
        System.out.println("请输入类名");
        String line = scan.next();
        Class cls = Class.forName(line);
        Object obj = cls.newInstance();
        System.out.println(obj);


    }
}
